package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper clueTrackRecordMapper;

    @Autowired
    private TbClueMapper clueMapper;

    @Override
    public int insertTbClueTrackRecord(ClueTrackRecordVo clueTrackRecordVo) {
        int rows = 0;
        try {
            TbClue clue = new TbClue();
            BeanUtils.copyProperties(clueTrackRecordVo, clue);
            clue.setId(clueTrackRecordVo.getClueId());
            clue.setStatus(TbClue.StatusType.FOLLOWING.getValue());

            int rows1 = clueMapper.updateTbClue(clue);

            TbClueTrackRecord clueTrackRecord = new TbClueTrackRecord();
            BeanUtils.copyProperties(clueTrackRecordVo, clueTrackRecord);

            clueTrackRecord.setCreateBy(SecurityUtils.getUsername());
            clueTrackRecord.setCreateTime(new Date());
            int rows2 = clueTrackRecordMapper.insertTbClueTrackRecord(clueTrackRecord);

            rows = rows1 + rows2;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return rows;
    }

    @Override
    public List<TbClueTrackRecord> selectTbClueRecordList(Long clueId) {
        List<TbClueTrackRecord> result ;
        try {
            result = clueTrackRecordMapper.selectTbClueRecordList(clueId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }
}
